package com.ayh.campusfun.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ayh.campusfun.common.PageResult;
import com.ayh.campusfun.common.core.jwt.JWTUtils;
import com.ayh.campusfun.common.service.impl.BaseServiceImpl;
import com.ayh.campusfun.common.utils.PraMap;
import com.ayh.campusfun.dao.FunShareDao;
import com.ayh.campusfun.entity.Attention;
import com.ayh.campusfun.entity.FunShare;
import com.ayh.campusfun.entity.GoodFriend;
import com.ayh.campusfun.entity.LikeFunTopic;
import com.ayh.campusfun.service.AttentionService;
import com.ayh.campusfun.service.FunShareService;
import com.ayh.campusfun.service.GoodFriendService;
import com.ayh.campusfun.service.LikeFunTopicService;
import com.ayh.campusfun.vo.ShareVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 趣事表(FunShare)表服务实现类
 *
 * @author ayh
 * @since 2021-03-20 20:42:01
 */
@Service
public class FunShareServiceImpl extends BaseServiceImpl<FunShare> implements FunShareService {
    @Resource
    private FunShareDao funShareDao;

    @Autowired
    private LikeFunTopicService likeFunTopicService;

    @Autowired
    private FunShareService funShareService;

    @Resource
    private JWTUtils jwtUtils;

    @Autowired
    private GoodFriendService goodFriendService;

    @Autowired
    private AttentionService attentionService;



    @Override
    public int shareClick(Map<String, Object> map) {
        return funShareDao.shareClick(map);
    }

    @Override
    public int forwardById(Long id) {
        return funShareDao.forwardById(id);
    }

    @Override
    public int commentById(Long id) {
        return funShareDao.commentById(id);
    }

    @Override
    public PageResult<FunShare> getFunList(HttpServletRequest request, Integer limit, Integer page, Integer funClassifyId) {

        String uid = jwtUtils.getUidFromRequest(request);

        Map<String,Object> map = new HashMap<>();
        map.put("limit",limit);
        map.put("page",page);
        map.put("order","send_time desc");
        map.put("funClassifyId",funClassifyId);
        if(funClassifyId == 1){
            map.put("likeNumGT",100);
        }
        if(funClassifyId == 2){
            List<Attention> attentionList = attentionService.findListByParam(PraMap.create().put("uid", uid).put("friendStatus",1));
            List<String> attentionIds = attentionList.stream().map(Attention::getAttentionId).collect(Collectors.toList());
            map.put("idIn",attentionIds);
            map.remove("funClassifyId");
        }
        if(funClassifyId == 999){
            List<GoodFriend> goodFriendList = goodFriendService.findListByParam(PraMap.create().put("uid", uid));
            List<String> friendIdList = goodFriendList.stream().map(GoodFriend::getFriendId).collect(Collectors.toList());
            map.put("idIn",friendIdList);
            map.remove("funClassifyId");
        }
        PageResult<FunShare> pageResult = funShareService.findAll(map);
        List<FunShare> funShareList = pageResult.getData();
        Map<String,Object> queryMap = new HashMap<>();

        pageResult.setData(funShareList.stream()
                .map(v->{
                    ShareVO shareVO = new ShareVO();
                    BeanUtil.copyProperties(v,shareVO);
                    boolean isClick = false;
                    boolean isAttention = false;
                    queryMap.put("uid",uid);
                    queryMap.put("funId",v.getId());
                    queryMap.put("funType",1);
                    LikeFunTopic likeFunTopic = likeFunTopicService.findByParam(queryMap);
                    queryMap.clear();
                    queryMap.put("uid",uid);
                    queryMap.put("attentionId",v.getUid());
                    Attention attention = attentionService.findByParam(queryMap);
                    if(likeFunTopic!=null) {
                        isClick = true;
                    }
                    if(attention!=null) {
                        isAttention = true;
                    }
                    shareVO.setIsClick(isClick);
                    shareVO.setIsAttention(isAttention);
                    return shareVO;
                }).collect(Collectors.toList()));
        return pageResult;
    }

    @Override
    public int addPlayNum(Long id) {
        return funShareDao.addPlayNum(id);
    }

    @Override
    public PageResult<FunShare> searchFun(HttpServletRequest request, Integer limit, Integer page, String keyWord) {
        String uid = jwtUtils.getUidFromRequest(request);

        Map<String,Object> map = new HashMap<>();
        map.put("limit",limit);
        map.put("page",page);
        map.put("order","send_time desc");
        map.put("keyWord",keyWord);
        PageResult<FunShare> pageResult = funShareService.findAll(map);
        return pageResult;
    }
}
